package com.gao;

import com.gao.mapper.Blog;
import com.gao.mapper.BlogMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;

import java.io.IOException;
import java.io.InputStream;

/**
 * 基于Xml配置
 *
 * @author GaoXi
 * @date 2022/3/3 20:04
 */
public class XmlBaseDemo {


    public static void main(String[] args) throws IOException {
        //放在resource目录下
        String resource = "mybatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        //development指定环境
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "development");
        SqlSession sqlSession = sqlSessionFactory.openSession();
        System.out.println(sqlSession);

        /** 获取映射器方式 */
        BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);
        Blog blog = mapper.selectBlog(1L);
        System.out.println(blog);

        /** 直接来方式 */
        Blog blog2 = (Blog) sqlSession.selectOne("com.gao.mapper.BlogMapper.selectBlog", 1L);
        System.out.println(blog2);

        /** 测试主键返回 */
/*        Blog blog1 = new Blog();
        blog1.setName("这一篇不错的Blog");
        int i = mapper.insertBlog(blog1);
        System.out.println(blog1);
        sqlSession.commit();*/
    }


}
